<?php
require_once '../config.php';
error_reporting(1);
class database {
	var $_sql = '';
	var $_connection = '';
	var $_cursor = null;
	
	function database() 
	{

		//$this->_connection = @mysql_connect( 'localhost', 'root', '');
		$this->_connection = @mysql_connect( DB_HOST,DB_USER ,DB_PASS );
		if (!$this->_connection ) 
		{
			die( "Khong the ket noi MySQL" );
		}
		
		mysql_query('SET NAMES UTF8', $this->_connection);
		
		$db = DB_NAME;
		if ($db != '' && !mysql_select_db( $db, $this->_connection )) 
		{
			die ( "Khong the mo CSDL $db: ".mysql_error() );
		}				
	}

	function get_error() 
	{
		return mysql_error();
	}

	function setQuery( $sql) 
	{
		$this->_sql = $sql;
	}

	function query() 
	{
	
		$this->_cursor = mysql_query( $this->_sql, $this->_connection );
		return $this->_cursor;
	}

	/* This method loads the first row returned by the query.*/
	function loadRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		$ret = null;
		if ($row = mysql_fetch_assoc( $cur )) {
			$ret = $row;
		}
		mysql_free_result( $cur );
		return $ret;
	}
	
	/**
	* This method loads the first field of the first row returned by the query.
	*
	* @return The value returned in the query or null if the query failed.
	*/
	function loadResult() {
		if (!($cur = $this->query())) {
			return null;
		}
		$ret = null;
		if ($row = mysql_fetch_row( $cur )) {
			$ret = $row[0];
		}
		mysql_free_result( $cur );
		return $ret;
	}
	/**
	* Load a  list of database rows
	*/
	function loadAllRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		$array = array();
		while ($row = mysql_fetch_assoc( $cur )) 
		{
			// ketqua = mang ket qua
			$array[] = $row;
		}
		mysql_free_result( $cur );
		return $array;
	}
	
	function disconnect() 
	{
		mysql_close( $this->_connection );
	}
	
	#Tra ve id sau khi insert xong. Chi su dung cho bang co khoa chinh la kieu int va tu dong tang.
	function getInsert_id() {
		return mysql_insert_id();
	}
}
?>